import math
import sys


def x_ppt_method(n, f=3, s=1, p=0, p2=None):
    if p2 is None:
        return (n + 2*p - f) / s + 1
    else:
        return (n + p + p2 - f) / s + 1


def x_tf_method(n, f=3, s=1, p='VALID'):
    xset = set(['VALID', 'SAME'])
    if p not in xset:
        print(f'p not in {xset}', file=sys.stderr)
        exit(1)
    if 'VALID' == p:
        r = math.ceil((n - f + 1.) / s)
    else:
        r = math.ceil(n / s)
    p = (r - 1) * s + f - n
    return r, p


if '__main__' == __name__:
    from python_ai.common.xcommon import sep

    xdata = [
        [[20], [20]],
        [[20, 3, 2], [20, 3, 2]],  # 9.5 vs 9, -1
        [[7], [7]],
        [[7, 3, 2], [7, 3, 2]],
        [[20, 3, 1, 1], [20, 3, 1, 'SAME']],
        [[20, 3, 2, 1, 0], [20, 3, 2, 'SAME']],
        [[7, 3, 1, 1], [7, 3, 1, 'SAME']],
        [[7, 3, 2, 1, 1], [7, 3, 2, 'SAME']]
    ]
    for i, (arg1, arg2) in enumerate(xdata):
        sep(i)
        print(f'{arg1}: {x_ppt_method(*arg1)}')
        print(f'{arg2}: {x_tf_method(*arg2)}')

    # ----------------0----------------
    # [20]: 18.0
    # [20]: (18, 0)
    # ----------------1----------------
    # [20, 3, 2]: 9.5
    # [20, 3, 2]: (9, -1)
    # ----------------2----------------
    # [7]: 5.0
    # [7]: (5, 0)
    # ----------------3----------------
    # [7, 3, 2]: 3.0
    # [7, 3, 2]: (3, 0)
    # ----------------4----------------
    # [20, 3, 1, 1]: 20.0
    # [20, 3, 1, 'SAME']: (20, 2)
    # ----------------5----------------
    # [20, 3, 2, 1, 0]: 10.0
    # [20, 3, 2, 'SAME']: (10, 1)
    # ----------------6----------------
    # [7, 3, 1, 1]: 7.0
    # [7, 3, 1, 'SAME']: (7, 2)
    # ----------------7----------------
    # [7, 3, 2, 1, 1]: 4.0
    # [7, 3, 2, 'SAME']: (4, 2)
